package com.elinshaw.wordcount;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * @author Administrator
 */
public class SplitBolt extends BaseRichBolt {

    private static final long serialVersionUID = 1L;
    private OutputCollector outputcollector;

    /**
     * bolt初始化方法，与spout的open()方法类似
     */
    @Override
    public void prepare(Map map, TopologyContext topologycontext, OutputCollector outputcollector) {
        this.outputcollector = outputcollector;
    }

    /**
     * 接收Tuple数据进行处理
     */
    @Override
    public void execute(Tuple tuple) {
        //获取发送过来的数据
        String sentence = tuple.getStringByField("line");
        //将数据以空格分割为单词数组
        String[] words = sentence.split(" ");
        //逐个将单词发射出去
        for (String word : words) {
            this.outputcollector.emit(new Values(word));
        }
    }

    /**
     * 字段声明
     */
    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputfieldsdeclarer) {
        outputfieldsdeclarer.declare(new Fields("word"));
    }

} 